package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.constant.SystemConstant;
import com.briup.pojo.Qrcode;
import com.briup.service.QrcodeService;
import com.briup.mapper.QrcodeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Objects;

/**
* @author Lenovo
* @description 针对表【manager_qrcode(溯源二维码信息表)】的数据库操作Service实现
* @createDate 2024-03-18 09:36:19
*/
@Service
@Slf4j
public class QrcodeServiceImpl extends ServiceImpl<QrcodeMapper, Qrcode>
    implements QrcodeService{
    @Autowired
    private QrcodeMapper qrcodeMapper;

    @Override
    public void addQRCode(String animalId, String url) {
        //新增还是修改
        //创建条件构造器
        LambdaQueryWrapper<Qrcode> qrcodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据动物ID查看二维码信息
        qrcodeLambdaQueryWrapper.eq(Qrcode::getQAnimalId,animalId);
        //查询二维码信息
        Qrcode qrcode = qrcodeMapper.selectOne(qrcodeLambdaQueryWrapper);
        //判断
        if (Objects.isNull(qrcode)){
            //新增数据i
            int insert = qrcodeMapper.insert(new Qrcode().setQAnimalId(animalId).setQImageUrl(url));
            log.info("插入行数{}",insert);
        }else{
            //不为空（修改操作）
            qrcode.setQImageUrl(url);
            //修改
            qrcodeMapper.updateById(qrcode);
        }

    }

}




